﻿Imports System.IO

Public Class frmViewClient

    Public _isPlaintiff As Boolean = False
    Public _isDefendant As Boolean = False
    Public _isThirdParty As Boolean = False
    Public _PlaintiffID As Long
    Public _DefendantID As Long
    Public _ThirdPartyID As Long
    Public _CaseID As Long

    Dim dt As New DataTable
    Dim j As Integer = 0
    Dim tmp As Integer

    Private Sub frmViewClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        j = 0
        If _CaseID <> 0 Then
            If _isPlaintiff Then
                getPlaintiff()
            ElseIf _isDefendant Then
                getDefendant()
            ElseIf _isThirdParty Then
                getThirdParty()
            End If
        Else
            MessageBox.Show("គ្មានពត៌មានបង្ហាញទេ", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Protected Sub getPlaintiff_byCaseID()
        Dim oPlaintiff As New cPlaintiff
        dt = oPlaintiff.select_Plaintiff_byCaseID(_CaseID)
    End Sub

    Protected Sub getDefendant_byCaseID()
        Dim oDefendant As New cDefendant
        dt = oDefendant.select_Defendant_byCaseID(_CaseID)
    End Sub

    Protected Sub getThirdParty_byCaseID()
        Dim oThirdParty As New cThirdParty
        dt = oThirdParty.select_ThirdParty_byCaseID(_CaseID)
    End Sub

    Protected Sub getPlaintiff()
        Try
            getPlaintiff_byCaseID()
            Dim i As Integer
            If dt.Rows.Count > 0 Then
                If _PlaintiffID <> 0 Then
                    For i = 0 To dt.Rows.Count - 1
                        If _PlaintiffID = dt.Rows(i)("PlaintiffID") Then
                            _PlaintiffID = dt.Rows(i)("PlaintiffID")
                            lblUserID.Text = dt.Rows(i)("UserID")
                            lblNo.Text = i + 1 & " of " & dt.Rows.Count

                            txtName.Text = dt.Rows(i)("PlaintiffName")
                            txtGender.Text = dt.Rows(i)("Gender")
                            dtpDOB.Value = dt.Rows(i)("DOB")
                            txtAddress.Text = dt.Rows(i)("Address")
                            txtTel.Text = dt.Rows(i)("Tel")
                            txtEmail.Text = dt.Rows(i)("Email")
                            txtIDCard.Text = dt.Rows(i)("IDCard")
                            'For Image
                            If dt.Rows(i)("Photo") IsNot DBNull.Value Then
                                Dim pic As Byte()
                                pic = dt.Rows(i)("Photo")
                                Dim streamPhoto As New MemoryStream(pic, True)
                                Dim bmp As Bitmap = New Bitmap(streamPhoto)
                                Dim bmp1 As Bitmap = New Bitmap(bmp)
                                picPhoto.Image = bmp1
                                streamPhoto.Close()
                            Else
                                picPhoto.Image = My.Resources.Photo
                            End If
                            Exit For
                        End If
                        j += 1
                    Next
                Else
                    MessageBox.Show("គ្មានពត៌មានបង្ហាញទេ", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub getDefendant()
        Try
            getDefendant_byCaseID()
            Dim i As Integer
            If dt.Rows.Count > 0 Then
                If _DefendantID <> 0 Then
                    For i = 0 To dt.Rows.Count - 1
                        If _DefendantID = dt.Rows(i)("DefendantID") Then
                            _DefendantID = dt.Rows(i)("DefendantID")
                            lblUserID.Text = dt.Rows(i)("UserID")
                            lblNo.Text = i + 1 & " of " & dt.Rows.Count

                            txtName.Text = dt.Rows(i)("DefendantName")
                            txtGender.Text = dt.Rows(i)("Gender")
                            dtpDOB.Value = dt.Rows(i)("DOB")
                            txtAddress.Text = dt.Rows(i)("Address")
                            txtTel.Text = dt.Rows(i)("Tel")
                            txtEmail.Text = dt.Rows(i)("Email")
                            txtIDCard.Text = dt.Rows(i)("IDCard")
                            'For Image
                            If dt.Rows(i)("Photo") IsNot DBNull.Value Then
                                Dim pic As Byte()
                                pic = dt.Rows(i)("Photo")
                                Dim streamPhoto As New MemoryStream(pic, True)
                                Dim bmp As Bitmap = New Bitmap(streamPhoto)
                                Dim bmp1 As Bitmap = New Bitmap(bmp)
                                picPhoto.Image = bmp1
                                streamPhoto.Close()
                            Else
                                picPhoto.Image = My.Resources.Photo
                            End If
                            Exit For
                        End If
                        j += 1
                    Next
                Else
                    MessageBox.Show("គ្មានពត៌មានបង្ហាញទេ", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub getThirdParty()
        Try
            getThirdParty_byCaseID()
            Dim i As Integer
            If dt.Rows.Count > 0 Then
                If _ThirdPartyID <> 0 Then
                    For i = 0 To dt.Rows.Count - 1
                        If _ThirdPartyID = dt.Rows(i)("ThirdPartyID") Then
                            _ThirdPartyID = dt.Rows(i)("ThirdPartyID")
                            lblUserID.Text = dt.Rows(i)("UserID")
                            lblNo.Text = i + 1 & " of " & dt.Rows.Count

                            txtName.Text = dt.Rows(i)("ThirdPartyName")
                            txtGender.Text = dt.Rows(i)("Gender")
                            dtpDOB.Value = dt.Rows(i)("DOB")
                            txtAddress.Text = dt.Rows(i)("Address")
                            txtTel.Text = dt.Rows(i)("Tel")
                            txtEmail.Text = dt.Rows(i)("Email")
                            txtIDCard.Text = dt.Rows(i)("IDCard")
                            'For Image
                            If dt.Rows(i)("Photo") IsNot DBNull.Value Then
                                Dim pic As Byte()
                                pic = dt.Rows(i)("Photo")
                                Dim streamPhoto As New MemoryStream(pic, True)
                                Dim bmp As Bitmap = New Bitmap(streamPhoto)
                                Dim bmp1 As Bitmap = New Bitmap(bmp)
                                picPhoto.Image = bmp1
                                streamPhoto.Close()
                            Else
                                picPhoto.Image = My.Resources.Photo
                            End If
                            Exit For
                        End If
                        j += 1
                    Next
                Else
                    MessageBox.Show("គ្មានពត៌មានបង្ហាញទេ", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub getPlaintiff_Move()
        If j >= 0 And j < dt.Rows.Count Then
            _PlaintiffID = dt.Rows(j)("PlaintiffID")
            lblUserID.Text = dt.Rows(j)("UserID")
            lblNo.Text = j + 1 & " of " & dt.Rows.Count

            txtName.Text = dt.Rows(j)("PlaintiffName")
            txtGender.Text = dt.Rows(j)("Gender")
            dtpDOB.Value = dt.Rows(j)("DOB")
            txtAddress.Text = dt.Rows(j)("Address")
            txtTel.Text = dt.Rows(j)("Tel")
            txtEmail.Text = dt.Rows(j)("Email")
            txtIDCard.Text = dt.Rows(j)("IDCard")
            'For Image
            If dt.Rows(j)("Photo") IsNot DBNull.Value Then
                Dim pic As Byte()
                pic = dt.Rows(j)("Photo")
                Dim streamPhoto As New MemoryStream(pic, True)
                Dim bmp As Bitmap = New Bitmap(streamPhoto)
                Dim bmp1 As Bitmap = New Bitmap(bmp)
                picPhoto.Image = bmp1
                streamPhoto.Close()
            Else
                picPhoto.Image = My.Resources.Photo
            End If
        Else
            j = tmp
        End If
    End Sub

    Protected Sub getDefendant_Move()
        If j >= 0 And j < dt.Rows.Count Then
            _DefendantID = dt.Rows(j)("DefendantID")
            lblUserID.Text = dt.Rows(j)("UserID")
            lblNo.Text = j + 1 & " of " & dt.Rows.Count

            txtName.Text = dt.Rows(j)("DefendantName")
            txtGender.Text = dt.Rows(j)("Gender")
            dtpDOB.Value = dt.Rows(j)("DOB")
            txtAddress.Text = dt.Rows(j)("Address")
            txtTel.Text = dt.Rows(j)("Tel")
            txtEmail.Text = dt.Rows(j)("Email")
            txtIDCard.Text = dt.Rows(j)("IDCard")
            'For Image
            If dt.Rows(j)("Photo") IsNot DBNull.Value Then
                Dim pic As Byte()
                pic = dt.Rows(j)("Photo")
                Dim streamPhoto As New MemoryStream(pic, True)
                Dim bmp As Bitmap = New Bitmap(streamPhoto)
                Dim bmp1 As Bitmap = New Bitmap(bmp)
                picPhoto.Image = bmp1
                streamPhoto.Close()
            Else
                picPhoto.Image = My.Resources.Photo
            End If
        Else
            j = tmp
        End If
    End Sub

    Protected Sub getThirdParty_Move()
        If j >= 0 And j < dt.Rows.Count Then
            _ThirdPartyID = dt.Rows(j)("ThirdPartyID")
            lblUserID.Text = dt.Rows(j)("UserID")
            lblNo.Text = j + 1 & " of " & dt.Rows.Count

            txtName.Text = dt.Rows(j)("ThirdPartyName")
            txtGender.Text = dt.Rows(j)("Gender")
            dtpDOB.Value = dt.Rows(j)("DOB")
            txtAddress.Text = dt.Rows(j)("Address")
            txtTel.Text = dt.Rows(j)("Tel")
            txtEmail.Text = dt.Rows(j)("Email")
            txtIDCard.Text = dt.Rows(j)("IDCard")
            'For Image
            If dt.Rows(j)("Photo") IsNot DBNull.Value Then
                Dim pic As Byte()
                pic = dt.Rows(j)("Photo")
                Dim streamPhoto As New MemoryStream(pic, True)
                Dim bmp As Bitmap = New Bitmap(streamPhoto)
                Dim bmp1 As Bitmap = New Bitmap(bmp)
                picPhoto.Image = bmp1
                streamPhoto.Close()
            Else
                picPhoto.Image = My.Resources.Photo
            End If
        Else
            j = tmp
        End If
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        tmp = j
        j = 0
        If _isPlaintiff Then
            getPlaintiff_Move()
        ElseIf _isDefendant Then
            getDefendant_Move()
        ElseIf _isThirdParty Then
            getThirdParty_Move()
        End If
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        tmp = j
        j -= 1
        If _isPlaintiff Then
            getPlaintiff_Move()
        ElseIf _isDefendant Then
            getDefendant_Move()
        ElseIf _isThirdParty Then
            getThirdParty_Move()
        End If
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        tmp = j
        j += 1
        If _isPlaintiff Then
            getPlaintiff_Move()
        ElseIf _isDefendant Then
            getDefendant_Move()
        ElseIf _isThirdParty Then
            getThirdParty_Move()
        End If
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        tmp = j
        j = dt.Rows.Count - 1
        If _isPlaintiff Then
            getPlaintiff_Move()
        ElseIf _isDefendant Then
            getDefendant_Move()
        ElseIf _isThirdParty Then
            getThirdParty_Move()
        End If
    End Sub
End Class